package leetcode.offer_two_special.backtrack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 1 2 3
 *
 * @author mazouri
 * @create 2021-09-18 22:34
 */
public class _83_permute {
    List<List<Integer>> res = new ArrayList<>();
    int[] nums;
//    Boolean[] visited;

    public List<List<Integer>> permute(int[] nums) {
        this.nums = nums;
//        visited = new Boolean[nums.length];
        LinkedList<Integer> track = new LinkedList<>();
        backtrack(track);
        return res;
    }

    private void backtrack(LinkedList<Integer> track) {
        //结束
        if (track.size() == nums.length) {
            res.add(new ArrayList<>(track));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            //排除不合理情况 重复
            if (track.contains(nums[i])) continue;
//            if (visited[i]) continue;

//            visited[i] = true;
            track.add(nums[i]);
            backtrack(track);
            track.removeLast();
//            visited[i] = false;
        }
    }
}
